import { ref } from "vue";
import { onPageScroll } from "@dcloudio/uni-app";
import { delayTask } from "@/utils/timer";

export const usePageScroll = (
  callback?: (scrollTop: number) => void,
  n?: number
) => {
  const pageIsScroll = ref(false);

  onPageScroll(e => {
    pageIsScroll.value = e.scrollTop > (n || 0);
    callback?.(e.scrollTop);
  });
  return { pageIsScroll, onPageScroll };
};

export const useViewScroll = () => {
  const scrollIndex = ref(-1);

  const scrollToIndex = (index: number) => {
    scrollIndex.value = index;
    delayTask(600, () => (scrollIndex.value = -1));
  };

  return { scrollIndex, scrollToIndex };
};
